package com.oocl.zero.task0710.property.filter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.oocl.zero.task0710.property.out.Printer;
import com.oocl.zero.task0710.property.parse.Parser;
import com.oocl.zero.task0710.property.parse.impl.ParserImpl;

public class CustomerFilter {
	public static final String ID_REGEX = "^[0-9]*$";
	public static final String EMAIL_REGEX = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
	public static final String GENDER_REGEX = "true|false";
	public static final String DATE_FORMAT = "yyyy-MM-dd";

	private boolean isErr=false;
	
	public  boolean isErr() {
		return isErr;
	}


	public static void main(String[] args) {
		
//		System.out.println( filterId("012") );
//		System.out.println( filterId("0as12") );
//		System.out.println( filterId("as") );
		
//		System.out.println( filterEmail("john@163.com") );
//		System.out.println( filterEmail("johndfm") );
//		System.out.println( filterEmail("john@16") );
		
//		System.out.println( filterGender("john@16") );
//		System.out.println( filterGender("true") );
//		System.out.println( filterGender("false") );
		
		String input="8 john john@163.com true			 1999-10-2";
		Parser parser = new ParserImpl();
		parser.doParse(input);
		Map<Integer, String> map = parser.getDataMap();
		
	}
	
	public void doFilter(Map<Integer, String> map){
		isErr=false;
		//check dataMap map.size()==5
		Set<Integer> keySet = map.keySet();
		// id(1) name(2) email(3) gender(4) birthday(5)
		String content;
		for (Integer key : keySet) {
			//
			content=map.get(key);
			//
			switch (key) {
			case 1:
				if(filterId(content)){
					Printer.print("id must be a number");
					isErr=true;
				}
				break;
			case 3:
				if(filterEmail(content)){
					Printer.print("Email input err");
					isErr=true;
				}
				break;
			case 4:
				if( filterGender(content) ){
					Printer.print("Gender input err");
					isErr=true;
				}
				break;
			case 5:
				if( filterDate(content) ){
					Printer.print("birthday date format input err");
					isErr=true;
				}
				break;
			default:
				break;
			}
		}
	}


	private static boolean filterDate(String content) {
		DateFormat df=new SimpleDateFormat(CustomerFilter.DATE_FORMAT);
		try {
			df.parse(content);
		} catch (ParseException e) {
			return true;
		}
		return false;
	}
	private static boolean filterGender(String content) {
		Pattern p=Pattern.compile(CustomerFilter.GENDER_REGEX);
		Matcher m = p.matcher(content);
		return !m.matches();
	}
	private static boolean filterEmail(String content) {
		Pattern p=Pattern.compile(CustomerFilter.EMAIL_REGEX);
		Matcher m = p.matcher(content);
		return !m.matches();
	}
	private static boolean filterId(String content) {
		 Pattern p=Pattern.compile(CustomerFilter.ID_REGEX);
		 Matcher m = p.matcher(content);
		 return !m.matches();
	}
}
